<?php
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// |
// +----------------------------------------------------------------------

namespace app\model;

use think\Model;

class Withdrawal extends Model
{
	protected $name               = 'withdrawal';
	protected $autoWriteTimestamp = true;
	protected $insert             = [];
	protected $update             = [];

	const WITHDRAWAL_STATUS_WAIT_AUDIT = 0; // 待审核
	const WITHDRAWAL_STATUS_WAIT_PAY   = 1; // 待打款
	const WITHDRAWAL_STATUS_COMPLETED  = 2; // 已打款
	const WITHDRAWAL_STATUS_REJECTED   = 3; // 已驳回
	const WITHDRAWAL_STATUS_DOING      = 4; // 打款中
	const WITHDRAWAL_STATUS_CANCEL     = 5; // 取消打款
	const WITHDRAWAL_STATUS_FAIL       = 6; // 打款失败

	public static array $withdrawalList = [
		self::WITHDRAWAL_STATUS_WAIT_AUDIT => '待审核',
		self::WITHDRAWAL_STATUS_WAIT_PAY   => '待打款',
		self::WITHDRAWAL_STATUS_COMPLETED  => '已打款',
		self::WITHDRAWAL_STATUS_REJECTED   => '已驳回',
		self::WITHDRAWAL_STATUS_DOING      => '待用户领取',
		self::WITHDRAWAL_STATUS_CANCEL     => '已撤销',
		self::WITHDRAWAL_STATUS_FAIL       => '转账失败',
	];

	const STATUS_WAIT      = 0; // 待审核
	const STATUS_COMPLETED = 1; // 通过
	const STATUS_REJECTED  = 2; // 拒绝

	public static $statusList = [
		self::STATUS_WAIT      => '待审核',
		self::STATUS_COMPLETED => '通过',
		self::STATUS_REJECTED  => '拒绝',
	];

	public function getWithdrawalMoneyAttr($value)
	{
		return $value ? $value / 100 : 0;
	}

	public function getCreateTimeAttr($value)
	{
		return date('Y-m-d H:i:s', $value);
	}

	public function getStatusTextAttr($value, $data)
	{
		$value = $data['status'];
		$text  = [
			0 => '<span class="layui-badge layui-bg-blue">待审核</span>',
			1 => '<span class="layui-badge layui-bg-green">通过</span>',
			2 => '<span class="layui-badge">驳回</span>',
		];
		if ($value == 2) {
			$text = $text[$value];
			$text .= '<i class="layui-icon" style="font-size: 20px; color: red;" onmouseover="layer.tips(\'' . $data['remark'] . '\',this,{tips: [1, \'#1A2426FF\'], time: 0});" onmouseout="layer.closeAll();">&#xe607;</i>';
			return $text;
		} else {
			return $text[$value];
		}
	}

	public function getWithdrawalStatusTextAttr($value, $data)
	{
		$value = $data['withdrawal_status'];
		$text  = [
			0 => '<span class="layui-badge layui-bg-blue">待审核</span>',
			1 => '<span class="layui-badge layui-bg-orange">待打款</span>',
			2 => '<span class="layui-badge layui-bg-green">已打款</span>',
			3 => '<span class="layui-badge">已驳回</span>',
			4 => '<span class="layui-badge layui-bg-orange">待用户领取</span>',
			5 => '<span class="layui-badge layui-bg-blue">已撤销</span>',
			6 => '<span class="layui-badge">转账失败</span>',
		];
		return $text[$value];
	}

	public function getImageAttr($value, $data)
	{
		$channel = $data['channel'];
		if ($channel != 0) {
			//
			$account = json_decode($data['account'], true);
			return $account['image'] ?? '';
		}
		return '';
	}

	public function user()
	{
		return $this->belongsTo('User', 'user_id')
			->bind([
				'nickname' => 'nickname',
				'mobile'   => 'mobile',
			]);
	}

	public function agent()
	{
		return $this->belongsTo(AdminAgent::class, 'user_id')
			->bind([
				'agent_name' => 'name',
			]);
	}

	public function record()
	{
		return $this->hasOne(WithdrawalAuto::class, 'withdrawal_id')
			->bind([
				'auto_status' => 'status',
				'auto_msg'    => 'msg',
				'auto_type'   => 'type',
			]);
	}
}
